#include<stdio.h>
/* Binary search with one test inside while loop */
int binsearch(int x,int [],int n);
int main()
{
  int i,x,n,y;
  int temp;
  int p[20];
  printf("enter the number of terms\n");
  scanf("%d",&n);
  i=n;
  y=0;
  
  while(i)
    {
      scanf("%d",&x);
      p[y]=x;
      i--;
      y++;
    }

  for(i=0;i<n-1;i++)
    {
      for(y=i+1;y<n;y++)
        {
          if(p[y]<p[i])
            {
             temp=p[y];
             p[y]=p[i];
             p[i]=temp;
            }
       }
   }
   printf("\nenter the element to be searched");
   scanf("%d",&x);
   printf("\n%d",binsearch(x,p,n));
   return 0;
}

int binsearch(int x,int p[],int n)
{
  int beg,end,mid;
  beg=0;
  end=n-1;
  mid=(beg+end)/2;
  while((beg<=end) && (x!=p[mid]))
    {
       if(x<p[mid])
         end=mid-1;
       else 
         beg=mid+1;
       
       mid=(beg+end)/2;
    }
      
  if(x==p[mid])
    return mid;
  else
    return -1;
}
           
     
     
           
 
